<?

function addSubscriber($subEmail,$subArea)
{

$mailListArray= getSubscribers();
addIfSubscriberNotExists($subEmail,$subArea,$mailListArray);

}


function getSubscribers(){
 $mailList= file_get_contents('data/mail-list');
 $mailListArray=unserialize($mailList);
 return $mailListArray;
}

function unSubscribe($md5Hash){

$unsubEmail="";
$sMailList= getSubscribers();
foreach($sMailList as $k=>$mailItem){
	if($md5Hash==md5($mailItem["email"])){
		$unsubEmail=$mailItem["email"];
		unset($sMailList[$k]);
		file_put_contents('data/mail-list',serialize($sMailList));
		mail("searchnirmal100@yahoo.co.in","User unsubscribed".$unsubEmail,$unsubEmail);
		break;
	}
}

}

function addIfSubscriberNotExists($sEmail,$sArea,$sMailList){

$exists=false;
foreach($sMailList as $k=>$mailItem){
	if($sEmail==$mailItem["email"]){
		$sMailList[$k]["area_need"]=$mailItem["area_need"].",".$sArea;
		if($sArea=="empty") $sMailList[$k]["area_need"]="";
		$exists=true;
		break;
	}

}

$sEmail=strtolower($sEmail);
if(!$exists){
	$sMailList[]=Array("mailHash"=>md5($sEmail),"email"=>$sEmail,"area_need"=>$sArea,"time"=>getCurrentTime());
}

file_put_contents('data/mail-list',serialize($sMailList));

}



function saveNotification($sMailList,$sEmail,$notifiedInfo){


foreach($sMailList as $k=>$mailItem){
	if($sEmail==$mailItem["email"]){

		$sMailList[$k]["notified"][]=$notifiedInfo;
		
		file_put_contents('data/mail-list',serialize($sMailList));
		break;
	}

}


}




function getCurrentTime(){

$timeNdate=date('Y-m-d H:i:s', time() + 41400);

return $timeNdate;

}


function processNotification(){

	$pressUrl="http://tneb.tnebnet.org/cpro/today.html";
	$input = @file_get_contents($pressUrl) or die("Could not access file: $url");
	$regexp = "<tr><td align=center><b>(.*)<\/b><\/td>\s*<td nowrap><b><pre>(.*)<\/pre><\/b><\/tr>";


	$areaList=Array();
	if(preg_match_all("/$regexp/siU", $input, $matches, PREG_SET_ORDER)) {
		foreach($matches as $match) {
			$eventList[]=Array("day"=>$match[1],"area"=>$match[2]);
		}
	}

  $subscriberList=getSubscribers();


  foreach($subscriberList as $k=>$item){

	$hasShutdown=0;
	$shutDownMatches=Array();
	$arrayAreas = explode(",",$item["area_need"]);
	$arrayAreas=array_unique($arrayAreas);
	foreach ($arrayAreas as $token) {
	
	foreach ($eventList as $event) {
	    $text=$event["area"];
	    if (stristr($text, $token) !== FALSE) {
	        $hasShutdown=1;
	        $shutDownMatches[]=Array("day"=>$event["day"],"area_need"=>$token);
	    }
	}
	
	}
	
	//echo "<pre>";
	//print_r($eventList);
	$message=Array();
	$itemMatched=0;
	$notifiedDates=Array();
	foreach ($shutDownMatches as $match) {
		$itemMatched=1;
		$message[]="Power Shutdown on ".$match["day"]." - On Area ".$match["area_need"];
		$notifiedDates[]=$match["day"];
	}
	$emailBody=join("<br>",$message);
	
	if($itemMatched==1) echo "<b>".$item["email"]."</b><br>".$emailBody."<br>";
	if($itemMatched==1){

		notifySubscriber($item["email"],$emailBody);
		saveNotification($subscriberList,$item["email"],$notifiedDates);
	} 
		  
  }
    
}

function notifySubscriber($toEmail,$content){


$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .='From:"Gyanmoti Spark" <nirmal@gyanmoti.in> '."\r\n";
$headers .="Bcc:kumar.nirmal.v@gmail.com\r\n";
$subject="EB Power Shutdown - Chennai - Notification";

$body="Dear Friend, You have subscribed for Chennai Power Shutdown  Notification from 'Spark Beta'  <br/>";
$body.="<br>".$content;
$body.="<br>For more details please refer this : <a href='http://tneb.tnebnet.org/cpro/today.html'>http://tneb.tnebnet.org/cpro/today.html</a>";
$body.="<br>";
$body.="Click <a href='http://gyanmoti.in/spark/subscribe.php?unsub=".md5($toEmail)."'>here</a> to unsubscribe";
$body.="<br/>With Regards<br/> Spark - Gyanmoti Team";


//echo $toEmail.$subject.$body.$headers;
if (@mail($toEmail, $subject, $body,$headers)) {
	return true;
 } else {

	return false;
 }

}


?>